package com.fw.utils;

import com.obs.services.ObsClient;
import com.obs.services.model.ObjectMetadata;

import java.io.InputStream;
import java.util.UUID;

public class HuaweiUtil {

    private static String endPoint = "obs.cn-east-3.myhuaweicloud.com";

    private static String ak = "VUEFUASYAKOE5RX93YMM";

    private static String sk = "yRrqH4Q1X9y4XX2HT4RnecvHxzy1SCYjtY1jDCA5";

    private static final String bucketLocation = "cn-north-4";

    private static final String bucketName = "shangjia1.0";
     private static ObsClient obsClient = null;
    public  static synchronized ObsClient getObsClient() {
        if (obsClient == null)
            obsClient =  new ObsClient(ak, sk, endPoint);
        return obsClient;
    }


    //上传文件
    public static String uploadFile(InputStream is, String fileName) {
        ObsClient obsClient = getObsClient();
        try {
            ObjectMetadata metadata = new ObjectMetadata();
            String  key = UUID.randomUUID().toString().replaceAll("-", "");

            //上传的文件的长度
            metadata.setContentLength(Integer.valueOf(is.available()).longValue());
            //指定该Object被下载时的内容编码格式
            metadata.setContentEncoding("utf-8");
            //文件的MIME，定义文件的类型及网页编码，决定浏览器将以什么形式、什么编码读取文件。如果用户没有指定则根据Key或文件名的扩展名生成，
            // 指定上传的内容类型。内容类型决定浏览器将以什么形式、什么编码读取文件。如果没有指定则根据文件的扩展名生成，如果没有扩展名则为默认值application/octet-stream。
            metadata.setContentType(AliUtil.getContentType(fileName));
            //同名文件可能被覆盖
            obsClient.putObject(bucketName, key, is,metadata);
            return "http://shangjia1.0.obs.cn-east-3.myhuaweicloud.com" + "/" + key;
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

}
